package com.dc.toolkit.json_jackson;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Description: Jackson Demo
 * @author: gdc
 * @date: 2021/6/7
 * @version 1.0
 */
public class JacksonTest {

	private final static ObjectMapper MAPPER = new ObjectMapper();

	static Student STUDENT = new Student().setId(1).setName("小明").setAge(14).setBirthday(new Date());
	static List<Student> STUDENT_LIST = Lists.newArrayList();
	static Map<String, Object> MAP = Maps.newHashMap();

	static {
		// List 赋值
		for (int i = 0; i < 5; i++) {
			Student student = new Student();
			student.setId(i);
			student.setName("小明" + i);
			student.setAge(10 + i);
			student.setBirthday(new Date());
			STUDENT_LIST.add(student);
		}

		// Map 赋值
		MAP.put("name", "lisi");
		MAP.put("sex", "男");
		MAP.put("age", 12);
	}

	/**
	 * json 与 单个对象的转换
	 */
	@Test
	public void test_one() {
		try {
			String jsonStr = MAPPER.writeValueAsString(STUDENT);
			System.out.println("json: "+ jsonStr);
			Student ss = MAPPER.readValue(jsonStr, Student.class);
			System.out.println(" 转化为对象：" + ss.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * json 与 List的转换
	 */
	@Test
	public void test_list() {
		try {
			// 将列表转化为 json
			String jsonStr = MAPPER.writeValueAsString(STUDENT_LIST);
			System.out.println("list To json\n"+ jsonStr);

			System.out.println("\n\njson To List");
			JavaType type = MAPPER.getTypeFactory().constructParametricType(ArrayList.class, Student.class);
			List<Student> studs = MAPPER.readValue(jsonStr, type);
			for (Student s : studs) {
				System.out.println(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *  json 与 Map的转换
	 *
	 */
	@Test
	public void test_map() {
		try {
			// Map TO JSON
			String mapJson = MAPPER.writeValueAsString(MAP);
			System.out.println(mapJson + "\r");

			// JSON TO Map
			JavaType type = MAPPER.getTypeFactory().constructType(Map.class);
			Map<String, Object> mapObject = MAPPER.readValue(mapJson, type);
			System.out.println(mapObject + "\r");
			System.out.println(mapObject.get("name") + " -- " + mapObject.get("age") + "\r");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 	对于比较复杂的树状 json
	 * 	通过 readTree() 读取字符串到内存
	 * 	get() 可以获取某个节点
	 * 	asInt() 可以将得到的节点转换为对应的数据类型 
	 */
	@Test
	public void test_readTree() {
		/*
		 * {
		 *     "results":[
		 *         {
		 *             "objectID":357,
		 *             "geoPoints":[
		 *                 {
		 *                     "x":504604.59802246094,
		 *                     "y":305569.9150390625
		 *                 }
		 *             ]
		 *         },
		 *         {
		 *             "objectID":358,
		 *             "geoPoints":[
		 *                 {
		 *                     "x":504602.2680053711,
		 *                     "y":305554.43603515625
		 *                 }
		 *             ]
		 *         }
		 *     ]
		 * }
		 */
		String test="{\"results\":[{\"objectID\":357,\"geoPoints\":[{\"x\":504604.59802246094,\"y\":305569.9150390625}]},{\"objectID\":358,\"geoPoints\":[{\"x\":504602.2680053711,\"y\":305554.43603515625}]}]}";
		try {
			JsonNode node = MAPPER.readTree(test);		// 将json 串已树状形式读入内存
			JsonNode results = node.get("results");		// 获取 results 节点下的信息
			for (int i = 0; i < results.size(); i++) {
				System.out.println("objectID=" + results.get(i).get("objectID").asInt());
				JsonNode geoPoints = results.get(i).get("geoPoints");	// 获取geoPoints 节点下的信息
				for (int j = 0; j < geoPoints.size(); j++) {
					System.out.println("x=>" + geoPoints.get(j).get("x").asDouble() + ",y=>" + geoPoints.get(j).get("y").asDouble());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 使用JsonUtil
	 */
	@Test
	public void test_JsonUtil() {
		String studentJson = JsonUtil.toJsonString(STUDENT);
		System.out.println("对象转JSON: " + studentJson);
		Student student = JsonUtil.json2Object(studentJson, Student.class);
		System.out.println("\nJSON转对象：" + student);

		String studentListjson = JsonUtil.toJsonString(STUDENT_LIST);
		System.out.println("\n列表转JSON: " + studentListjson);

		List<Student> studentList = JsonUtil.json2Object(studentListjson, List.class);
		System.out.println("\nJSON转列表: " + studentList);
	}
}
